package com.excesys.exsecs.energy.controller;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ArrayUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import com.excesys.exsecs.common.core.util.R;
import com.excesys.exsecs.common.excel.annotation.RequestExcel;
import com.excesys.exsecs.common.excel.annotation.ResponseExcel;
import com.excesys.exsecs.common.log.annotation.SysLog;
import com.excesys.exsecs.energy.entity.RegDev;
import com.excesys.exsecs.energy.service.RegDevService;
import com.excesys.exsecs.energy.vo.RegDevExcelVO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springdoc.api.annotations.ParameterObject;
import org.springframework.http.HttpHeaders;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 能耗设备注册表
 *
 * @author pig
 * @date 2024-02-20 17:10:34
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/regdev")
@Tag(description = "regDev", name = "能耗设备注册表")
@SecurityRequirement(name = HttpHeaders.AUTHORIZATION)
public class RegDevController {

    private final RegDevService regDevService;

    /**
     * 分页查询
     *
     * @param page   分页对象
     * @param regDev 能耗设备注册表
     * @return
     */
    @Operation(summary = "分页查询", description = "分页查询")
    @GetMapping("/page")
    @PreAuthorize("@pms.hasPermission('twinops_regDev_view')")
    public R getRegDevPage(@ParameterObject Page page, @ParameterObject RegDev regDev) {
        return R.ok(regDevService.queryPage(page, regDev));
    }

    /**
     * 通过unitNo集合查询
     *
     * @param unitNos
     * @return
     */
    @Operation(summary = "通过unitNo集合查询", description = "通过unitNo集合查询")
    @PostMapping("/listByUnitNos")
    @PreAuthorize("@pms.hasPermission('twinops_regDev_view')")
    public R listByUnitNos(@RequestBody List<String> unitNos) {
        return R.ok(regDevService.queryRegDevList(unitNos));
    }


    /**
     * 通过id查询能耗设备注册表
     *
     * @param id id
     * @return R
     */
    @Operation(summary = "通过id查询", description = "通过id查询")
    @GetMapping("/{id}")
    @PreAuthorize("@pms.hasPermission('twinops_regDev_view')")
    public R getById(@PathVariable("id") Long id) {
        return R.ok(regDevService.getById(id));
    }

    /**
     * 查询未注册设备列表
     *
     * @return R
     */
    @Operation(summary = "查询未注册设备列表", description = "查询未注册设备列表")
    @GetMapping("/unRegisterDev")
    @PreAuthorize("@pms.hasPermission('twinops_regDev_view')")
    public R unRegisterDev(String devModel) {
        return R.ok(regDevService.unRegisterDev(devModel));
    }


    /**
     * 新增能耗设备注册表
     *
     * @param regDevs 能耗设备注册表
     * @return R
     */
    @Operation(summary = "新增能耗设备注册表", description = "新增能耗设备注册表 ")
    @SysLog("新增能耗设备注册表")
    @PostMapping
    @PreAuthorize("@pms.hasPermission('twinops_regDev_add')")
    public R save(@RequestBody List<RegDev> regDevs) {
        //首先要查询注册的设备
        for (RegDev regDev : regDevs) {
            List<RegDev> list = regDevService.list(Wrappers.<RegDev>lambdaQuery().eq(RegDev::getUnitNo, regDev.getUnitNo()));
            if (list.size() > 0) {
                return R.ok(false, "设备已注册，请勿重复注册！");
            }
        }
        return R.ok(regDevService.saveRegDev(regDevs));
    }


    /**
     * 批量新增能耗设备注册表
     *
     * @param regDevs 能耗设备注册表
     * @return R
     */
    @Operation(summary = "批量新增能耗设备注册表", description = "批量新增能耗设备注册表 ")
    @SysLog("批量新增能耗设备注册表")
    @PostMapping("/saveBatchRegDev")
    @PreAuthorize("@pms.hasPermission('twinops_regDev_add')")
    public R saveBatchRegDev(@RequestBody List<RegDev> regDevs) {
        return R.ok(regDevService.saveRegDev(regDevs));
    }


    /**
     * 修改能耗设备注册表
     *
     * @param regDev 能耗设备注册表
     * @return R
     */
    @Operation(summary = "修改能耗设备注册表")
    @SysLog("修改能耗设备注册表")
    @PutMapping
    @PreAuthorize("@pms.hasPermission('twinops_regDev_edit')")
    public R updateById(@RequestBody RegDev regDev) {
        return R.ok(regDevService.updateById(regDev));
    }

    /**
     * 通过id删除能耗设备注册表
     *
     * @param ids id列表
     * @return R
     */
    @Operation(summary = "通过id删除能耗设备注册表 ", description = "通过id删除能耗设备注册表 ")
    @SysLog("通过id删除能耗设备注册表")
    @DeleteMapping
    @PreAuthorize("@pms.hasPermission('twinops_regDev_del')")
    public R removeById(@RequestBody Long[] ids) {
        return R.ok(regDevService.removeBatchByIds(CollUtil.toList(ids)));
    }


    /**
     * 导出excel 表格
     *
     * @param regDev 查询条件
     * @param ids    导出指定ID
     * @return excel 文件流
     */
    @ResponseExcel
    @GetMapping("/export")
    @PreAuthorize("@pms.hasPermission('twinops_regDev_export')")
    public List<RegDev> export(RegDev regDev, Long[] ids) {
        return regDevService.list(Wrappers.lambdaQuery(regDev).in(ArrayUtil.isNotEmpty(ids), RegDev::getId, ids));
    }

    /**
     * 导入excel 表格
     *
     * @return excel 文件流
     */
    @PostMapping("/import")
    public R importRegDev(@RequestExcel List<RegDevExcelVO> excelVOList, BindingResult bindingResult) {
        return regDevService.batchImport(excelVOList, bindingResult);
    }
}